import numpy as np
import matplotlib.pyplot as plt
import os
import timeit

COMPONENT_COUNT = 100
CODE_COUNT = 800
PATCH_COUNT = 400000
PLOT_COUNT = 800
EPOCH_COUNT = 16
SPARSITY_PARAMETER = 4.0
NON_NEGATIVE = True

start = timeit.default_timer()

sortedFile = 'codes/codesSortednn' + str(NON_NEGATIVE) + 'lamb' + str(SPARSITY_PARAMETER) + 'comps' + str(COMPONENT_COUNT) + 'codes' + str(CODE_COUNT) + 'patches' + str(PATCH_COUNT) + 'epochs' + str(EPOCH_COUNT) + '.npy'
outputFolder = 'codes/patchesnn' + str(NON_NEGATIVE) + 'lamb' + str(SPARSITY_PARAMETER) + 'comps' + str(COMPONENT_COUNT) + 'codes' + str(CODE_COUNT) + 'patches' + str(PATCH_COUNT) + 'epochs' + str(EPOCH_COUNT) + '/'

if not os.path.exists(outputFolder):
    os.makedirs(outputFolder)

patches = np.load('../patches.npy')
codesSorted = np.load(sortedFile)

def plotpatches(n, fig):
    for i in range(1, 8):
        ax = fig.add_subplot(170 + i)
        ax.imshow(patches[codesSorted[-i, n], 8:48, 8:48, 0], cmap = 'gray', interpolation = 'none')
        ax.set_axis_off()

for n in range(0, PLOT_COUNT):
    fig = plt.figure()
    plotpatches(n, fig)
    fileName = outputFolder + str(n)
    fig.savefig(fileName)
    plt.close(fig)
    del fig

stop = timeit.default_timer()

print('Time: ', stop - start)

